package com.youwu.weather.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.youwu.weather.service.CityDataService;
import com.youwu.weather.util.ReadExcel;
import com.youwu.weather.vo.City;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

@Service
public class CityDataServiceImpl implements CityDataService {

    private static Logger logger = LoggerFactory.getLogger(CityDataServiceImpl.class);
    private static final String CITY_DATA = "CITY_DATA";

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<City> listCity() {

        List<City> cityList = new ArrayList<>();
        String strBody = null;
        ListOperations<String, String> ops = stringRedisTemplate.opsForList();

        // 先判断redis缓存是否有城市数据，有直接取
        if (stringRedisTemplate.hasKey(CITY_DATA)) {

            logger.info("Redis has city list");
            strBody = ops.index(CITY_DATA, 0);
            cityList = JSONObject.parseArray(strBody, City.class);
        } else {

            // 读取excel数据
            File file = null;
            try {
                file = ResourceUtils.getFile("classpath:static/excel/city.xls");
                List excel = ReadExcel.readExcel(file);
                // 从第三行开始读取
                for (int i = 2; i < excel.size(); i++) {
                    List list = (List) excel.get(i);
                    City city = new City();
                    // 将数据转为Java对象
                    if (null != list && list.size() > 0) {
                        city.setCityId(list.get(0).toString());
                        city.setCityEn(list.get(1).toString());
                        city.setCityCn(list.get(2).toString());
                        city.setCountryCode(list.get(3).toString());
                        city.setCountryEn(list.get(4).toString());
                        city.setCountryCn(list.get(5).toString());
                        city.setProvinceEn(list.get(6).toString());
                        city.setProvinceCn(list.get(7).toString());
                        city.setAdminDistrictEn(list.get(8).toString());
                        city.setAdminDistrictCn(list.get(9).toString());
                        city.setLatitude(Double.valueOf(list.get(10).toString()));
                        city.setLongitude(Double.valueOf(list.get(11).toString()));
                        city.setAdCode(list.get(12).toString());
                        cityList.add(city);
                    }
                }
                ops.rightPushAll(CITY_DATA, JSON.toJSONString(cityList));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        return cityList;
    }
}
